//引入ajax
import {report,Qrcode} from "./request.js"
let span =document.querySelector("span")
let img = document.querySelector("img")
let pp = document.querySelector("p")
let myBox = document.querySelector(".myBox")
let dialog = document.querySelector(".dialog")
let timer = ["09:00-12:00","12:00-14:30","14:30-17:00"]
let nowDay = new Date()
let title = document.querySelector(".title")
let mask = document.querySelector(".mask")
async function MyBook(){
    let data = {
        phone:sessionStorage.getItem("phone"),
        name:sessionStorage.getItem("name")
    }
    let res = await report(data)
    let index = 0
    res.result.sort((a,b)=>{
        return (new Date(b.day_time)-new Date(a.day_time))
    });
    
    res.result.forEach(function(ele){
        console.log(ele.day_time);
        data.id = ele.id
        let nowDay = new Date(`${ele.day_time} ${timer[ele.show_id -1].slice(6)}`)
        console.log(nowDay);
        console.log(ele.day_time);//预约的时间
        console.log(new Date(ele.day_time));
        // console.log(nowDay - +new Date(ele.day_time));
        if(ele.day_time !== "undefined" && nowDay - new Date() >= 0){
            index++
            // myBox.innerHTML = ""
            myBox.innerHTML += `
            <div class="box">
                <span>姓名:${res.result[0].name}</span>
                <span>日期：${ele.day_time}</span>
                <span>场次:${timer[ele.show_id -1]}</span>
                <span class="check">查看二维码</span>
                </div>
                                `
                                console.log(ele.day_time);
                                console.log(res);
        }
        title.innerHTML = `
        <h2>我的预约</h2>
        <span>共：${index} 条</span>
        `
    })
    if(res.result.length == 0){
        myBox.innerHTML += `
        <div class="box">
        <span>无记录</span>
        </div>
                        `
                        title.innerHTML = `
                        <h2>我的预约</h2>
                        <span>共：${index} 条</span>
                        `
    }
    let check = document.querySelectorAll(".check")
    console.log(check);
    check.forEach(function(ele){
        ele.addEventListener("click",async function(){
            mask.style.display = "block"
            data.token = sessionStorage.getItem("token")
            let res = await Qrcode(data)
            console.log(res);
            console.log(check);
            img.src = res.data.data;
        })
    })
}
MyBook()
mask.addEventListener("click",function(){
    mask.style.display = "none"
})